// stdafx.cpp : source file that includes just the standard includes
//	UIlib.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "StdAfx.h"
//#include "CommonListModel.h"


namespace DuiLib
{

	CCommonListModel::CCommonListModel(int rows, int cols)
	{
		m_ColumnCount = cols;

		initHeadData();

		insertRows(0, rows);
	}

	CCommonListModel::~CCommonListModel()
	{

	}

	bool CCommonListModel::hasIndex(int row, int col, const CModelIndex& parent) const
	{
		if (row<0 || col<0 || parent!=CModelIndex())
		{
			return false;
		}

		return row < m_Data.size() && col < m_ColumnCount;
	}

	CModelIndex CCommonListModel::index(int row, int col, const CModelIndex& parent) const
	{
		if (row<0 || col<0)
		{
			return CModelIndex();
		}

		return createIndex(row, col);
	}

	CModelIndex CCommonListModel::sibling(int row, int col, const CModelIndex& idx) const
	{
		if (row < 0 || col < 0 || row >= m_Data.size() || col >= m_ColumnCount)
		{
			return CModelIndex();
		}

		return createIndex(row, col);
	}

	CModelIndex CCommonListModel::parent(const CModelIndex& child) const
	{
		return CModelIndex();
	}

	bool CCommonListModel::hasChild(const CModelIndex& parent) const
	{
		return false;
	}

	int CCommonListModel::rowCount(const CModelIndex& parent) const
	{
		return m_Data.size();
	}

	int CCommonListModel::columnCount(const CModelIndex& parent) const
	{
		return m_ColumnCount;
	}

	CVar CCommonListModel::data(const CModelIndex& index) const
	{
		if (index.row() < 0 || index.row() >= rowCount() ||
			index.column() < 0 || index.column() >= columnCount())
		{
			return CVar();
		}

		return m_Data[index.row()][index.column()];
	}

	bool CCommonListModel::setData(const CModelIndex& index, const CVar& var)
	{
		if (index.row() < 0 || index.row() >= rowCount() ||
			index.column() < 0 || index.column() >= columnCount())
		{
			return false;
		}

		CVar old = m_Data[index.row()][index.column()];

		m_Data[index.row()][index.column()] = var;

		dataChanged(index, old, var);
		return true;
	}

	CVar CCommonListModel::headerData(int section, Orientation orient) const
	{
		if (section >= m_ColumnCount)
		{
			return CVar();
		}

		return m_HeadData[section];
	}

	bool CCommonListModel::setHeaderData(int section, Orientation orient, const CVar& var)
	{
		if (section >= m_ColumnCount)
		{
			return false;
		}

		CVar old = m_HeadData[section];

		m_HeadData[section] = var;

		headerDataChanged(section, orient, old, var);

		return true;
	}

	bool CCommonListModel::insertRows(int row, int count, const CModelIndex& parent)
	{
		if (row < 0 || parent != CModelIndex())
		{
			return false;
		}

		rowsBeforeInsert(row, count, parent);

		VarLine lineData;
		for (int i = 0; i < m_ColumnCount; i++)
		{
			lineData.push_back(CVar());
		}

		VarMatrix::iterator it = m_Data.begin() + row;
		for (int i = 0; i < count; i++)
		{
			it = m_Data.insert(it, lineData);
		}

		rowsInserted(row, count, parent);
		return true;
	}

	bool CCommonListModel::removeRows(int row, int count, const CModelIndex& parent)
	{
		if (row < 0 || row >= rowCount() || count > rowCount() || parent!=CModelIndex())
		{
			return false;
		}

		rowsBeforeRemove(row, count, parent);

		VarMatrix::iterator it = m_Data.begin() + row;
		for (int i = 0; i < count; i++)
		{
			it = m_Data.erase(it);
		}

		rowsRemoved(row, count, parent);
		return true;
	}

	bool CCommonListModel::removeAll()
	{
		int rows = rowCount();

		m_Data.clear();

		allRemoved(rows);
		return true;
	}

	void CCommonListModel::initHeadData()
	{
		for (int i = 0; i < m_ColumnCount; i++)
		{
			m_HeadData.push_back(CVar());
		}
	}

}